//app.js
var stat = require('utils/mdk_stat.js')
import Http from './utils/http.js';
App({
  onLaunch: function () {
    wx.checkSession({
      //登录态未过期
      success: (e) => {
        //登录态未过期但是没有获取到token
        if (!wx.getStorageSync('token')) {
          this.wxLogin();
        }
      },
      //登录态过期了
      fail: () => {
        this.wxLogin();
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
    // 获取系统字典
    Http.Request('GET', `/api/admin/dictionary/getAllEnums/v1`).then(res => {
      this.globalData.dictionary = res;
    })
  },
  //存储用户昵称和头像
  userInfoReadyCallback(res){
    const userInfo = JSON.parse(res.rawData);
    this.globalData.userInfo = userInfo;
    wx.setStorageSync('userInfo', userInfo);
    const data = {
      custPortrait: userInfo.avatarUrl,
      username: userInfo.nickName
    }
    Http.Request('post', `/api/customer/uploadCustPortrait/v1`, data)
  },
  wxLogin: function () {
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          const data = {
            wxCode: res.code
          }
          Http.Request('POST', `/api/sso/CUSTOMER/login`, data).then(res => {
            wx.setStorageSync('token', res.token);
          }).catch(err => {
            wx.showToast({
              title: err,
              icon: "none",
              duration: this.globalData.duration
            })
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null,
    shoppingCart:[],
    domain: 'https://dengwominsu.zgdwms.com',
    duration: 1500,//提示框显示时间
    defaultHtml: '<div>暂无内容</div>',
    pageSize:2,//分页每次加载的条数
    dictionary:{},//字典数据
  },
  globalFn: {
    onShareAppMessage() {
      return {
        title: '等我民宿公寓',
        path: '/page/home',
        success: function (res) {
          // 转发成功
        },
        fail: function (res) {
          // 转发失败
        }
      }
    },
    //比较客户端版本
    compareVersion: function (v1, v2) {
      v1 = v1.split('.')
      v2 = v2.split('.')
      const len = Math.max(v1.length, v2.length)
      while (v1.length < len) {
        v1.push('0')
      }
      while (v2.length < len) {
        v2.push('0')
      }
      for (let i = 0; i < len; i++) {
        const num1 = parseInt(v1[i])
        const num2 = parseInt(v2[i])
        if (num1 > num2) {
          return 1
        } else if (num1 < num2) {
          return -1
        }
      }
      return 0
    },
    //重新加载页面
    reLoad: function (fn, msg ='获取信息失败') {
      wx.showModal({
        title: msg,
        content: '是否重新加载页面',
        success(res) {
          if (res.confirm) {
            fn()
          } else if (res.cancel) {
          
          }
        }
      })
    }
  }
})